﻿#region Librerias
using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
#endregion

/// <summary>
/// Class Role, define los tipos de roles deacuerdo a los programas
/// </summary>

#region Class

public class clsRole : DAC
{
    #region Variables

    private int idRole, flgUpdate, flgInsert, flgDelete;
    private String sName, sDescription;
    private const String tabla = "dbo.tblRole";

    #endregion

    #region Builder

    /// <summary>
    /// Constructor
    /// </summary>
    public clsRole() { }

    #endregion

    #region Methods

    #region Consultations

    public DataSet SeeAllTheRecords()
    {
        String query = "SELECT row_number() over(order by " + tabla + ".idRole) As NUMBER, ";
        query += tabla + ".idRole As ID, " + tabla + ".flgInsert As FLAGINSERT, ";
        query += tabla + ".flgUpdate As FLAGUPDATE, " + tabla + ".flgDelete As FLAGDELETE, ";
        query += tabla + ".sName As NAME, " + tabla + ".sDescription As DESCRIPTION ";
        query += "FROM " + tabla;
        return this.ExecuteQuery(query);
    }

    public DataSet SeeAllTheRecords(int startRecord, int maxRecord, String text)
    {
        String query = "SELECT row_number() over(order by " + tabla + ".idRole) As NUMBER, ";
        query += tabla + ".idRole As ID, " + tabla + ".flgInsert As FLAGINSERT, ";
        query += tabla + ".flgUpdate As FLAGUPDATE, " + tabla + ".flgDelete As FLAGDELETE, ";
        query += tabla + ".sName As NAME, " + tabla + ".sDescription As DESCRIPTION ";
        query += "FROM " + tabla;
        if (!text.Equals(""))
        {
            query += " WHERE " + tabla + ".sName LIKE '%" + text + "%' ";
        }
        if (startRecord == 0 && maxRecord == 0)
        {
            return this.ExecuteQuery(query);
        }
        else
        {
            return this.ExecuteQuery(query, startRecord, maxRecord);
        }
    }

    public DataSet ConsultID(String id)
    {
        String query = "SELECT ";
        query += tabla + ".idRole As ID, " + tabla + ".flgInsert As FLGINSERT, ";
        query += tabla + ".flgUpdate As FLGUPDATE, " + tabla + ".flgDelete As FLGDELETE, ";
        query += tabla + ".sName As NAME, " + tabla + ".sDescription As DESCRIPTION ";
        query += "FROM " + tabla;
        query += " WHERE " + tabla + ".idRole =" + id;
        return this.ExecuteQuery(query);
    }

    #endregion

    public int Insert()
    {
        String query = "INSERT INTO " + tabla;
        query += "(flgInsert, flgUpdate, flgDelete, sName, sDescription) ";
        query += "VALUES ('{0}','{1}','{2}','{3}','{4}')";

        String sql = String.Format(query, this.FlgInsert, this.FlgUpdate, this.FlgDelete, this.SName, this.SDescription);
        return this.ExecuteDML(sql, "no");
    }

    public int Update()
    {
        String query = "UPDATE " + tabla + " SET ";
        query += tabla + ".flgInsert=" + this.FlgInsert + ", " + tabla + ".flgUpdate=" + this.FlgUpdate + ", ";
        query += tabla + ".flgDelete=" + this.FlgDelete + ", ";
        query += tabla + ".sName='" + this.SName + "', " + tabla + ".sDescription='" + this.SDescription + "' ";
        query += "WHERE ";
        query += tabla + ".idRole=" + this.ID;
        return this.ExecuteDML(query, "no");
    }

    public int Delete()
    {
        String query = "DELETE FROM " + tabla + " WHERE " + tabla + ".idRole={0}";
        String sql = String.Format(query, this.ID);
        return this.ExecuteDML(sql, "no");
    }

    #endregion

    #region Setters and Getters

    public int FlgDelete
    {
        get { return flgDelete; }
        set { flgDelete = value; }
    }

    public int FlgInsert
    {
        get { return flgInsert; }
        set { flgInsert = value; }
    }

    public int FlgUpdate
    {
        get { return flgUpdate; }
        set { flgUpdate = value; }
    }

    public int ID
    {
        get { return idRole; }
        set { idRole = value; }
    }

    public String SDescription
    {
        get { return sDescription; }
        set { sDescription = value; }
    }

    public String SName
    {
        get { return sName; }
        set { sName = value; }
    }

    #endregion

}

#endregion